package com.gzwanchuang.xhgj.ui.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import com.gzwanchuang.xhgj.R;

import kale.ui.view.dialog.BaseCustomDialog;
import kale.ui.view.dialog.BaseEasyDialog;
import kale.ui.view.dialog.EasyDialog;

/**
 * Created by AndroidIntexh1 on 2018/9/25.
 */

public class SelectRenDialog extends BaseCustomDialog {

    public static final String KEY_DATA = "KEY_DATA";
    public static final String KEY_TITLE = "KEY_TITLE";
    private SelectRenDialog.OnDialogClickListener dialogListener;


    /**
     * 继承自{@link EasyDialog.Builder}以扩展builder
     */
    public static class Builder extends BaseEasyDialog.Builder<Builder> {

        private SelectRenDialog.OnDialogClickListener listener;
        private Bundle bundle = new Bundle();

        public Builder(@NonNull Context context) {
            super(context);
        }


        public Builder setData(String data) {
            bundle.putString(KEY_DATA, data);
            return this;
        }

        public Builder setTitle(String title) {
            bundle.putString(KEY_TITLE, title);
            return this;
        }


        public Builder setListen(SelectRenDialog.OnDialogClickListener title) {
            listener = title;
            return this;
        }

        @NonNull
        @Override
        protected EasyDialog createDialog() {
            SelectRenDialog dialog = new SelectRenDialog();
            dialog.setArguments(bundle); // 增加自己的bundle
            dialog.dialogListener = listener;
            return dialog;
        }
    }


    @Override
    protected int getLayoutResId() {
        return R.layout.dialog_select_bink;
    }

    @Override
    protected void bindViews(View view) {
       findView(R.id.tv_tier).setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               dialogListener.onClickTwo();
               dismiss();
           }
       });
        findView(R.id.tv_yinhang).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogListener.onClickOne();
                dismiss();
            }
        });
        findView(R.id.tv_quxiao).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    @Override
    protected void setViews() {
        setBackground();
        setLayout();

    }

    /**
     * 强制取消背景，保持有透明
     */
    private void setBackground() {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable()); // 去除dialog的背景，即透明
//        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0xffffffff)); // 设置白色背景
//        getDialog().getWindow().setBackgroundDrawableResource(R.drawable.dialog_shop_jion_bg); // 设置背景
    }


    /**
     * 也可通过setLayout来设置：
     * getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
     */
    private void setLayout() {
        Window window = getDialog().getWindow();
        final WindowManager.LayoutParams lp = window.getAttributes();

        // 强制宽高
//        int padding = getResources().getDimensionPixelOffset(R.dimen.dialog_padding);
        lp.width = getScreenWidth(getActivity());
//        lp.height = getResources().getDimensionPixelOffset(R.dimen.dialog_height);

        lp.gravity = Gravity.BOTTOM; // 设置展示的位置
        window.setAttributes(lp);
    }

    @Override
    protected void modifyAlertDialogBuilder(AlertDialog.Builder builder) {
        super.modifyAlertDialogBuilder(builder);
    }

    public static int getScreenWidth(Activity activity) {
        final DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }


    /**
     * 监听输入框内容
     */
    public interface OnDialogClickListener {
        /**
         * Called when menu_text_color view has been clicked.
         *
         *
         */
        void onClickOne();
        void onClickTwo();
    }



}
